package com.itboye.eweilisten.adapter;

import android.content.Context;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.MyBookBean;
import com.itboye.eweilisten.util.DateUtil;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.widget.TimeFormat;
import com.itboye.eweilisten.util.widget.XImageLoader;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by itboye on 2017/3/13.
 */

public class BooksAdapter extends BaseAdapter {

    Context context;
    List<MyBookBean.ListInfo> bean;

    public BooksAdapter(Context context, List<MyBookBean.ListInfo> bean) {
        this.context = context;
        this.bean = bean;
    }

    @Override
    public int getCount() {
        return bean.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHouder houder = null;
        if (convertView == null) {
            houder = new ViewHouder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_prienter_books, null);
            houder.imgBooks = (ImageView) convertView.findViewById(R.id.imgBooks);
            houder.myBooksTitle = (TextView) convertView.findViewById(R.id.myBooksTitle);
            houder.myBooksCount = (TextView) convertView.findViewById(R.id.myBooksCount);
            houder.validtime = (TextView) convertView.findViewById(R.id.validtime);
            convertView.setTag(houder);
        } else {
            houder = (ViewHouder) convertView.getTag();
        }
        houder.myBooksTitle.setText(bean.get(position).getName());
        houder.myBooksCount.setText(bean.get(position).getSynopsis());

        long validtime = Long.valueOf(bean.get(position).getExpire_time());
        houder.validtime.setText("有效期至:" + new SimpleDateFormat("yyyy年MM月dd日").format(new Date(validtime * 1000L)));

        XImageLoader.load(Const.IMAGE_HEAD + bean.get(position).getImg_main(), houder.imgBooks);
        return convertView;
    }

    public class ViewHouder {

        private TextView myBooksTitle;
        private TextView myBooksCount;
        private ImageView imgBooks;
        private TextView validtime;
    }
}
